Skip to main content

MiniProgram SDK

tip

This SDK is only applicable to WeChat mini program, other mini programs including Alipay, Baidu, DingDing and TouTiao are preparing, will be released soon. To provide a better integration for use in mini program applications, MiniProgram SDK builds on JavaScript SDK. Much of the JavaScript SDK functionality is also available for the MiniProgram SDK to use. Please reference JavaScript SDK.

Notice

For users who needs to use metric analysis, please upgrade MiniProgram SDK to version 2.0.1 . From this version, we support sending custom events.

SDK quick links

In addition to this reference guide, we provide source code, API reference documentation, and sample applications at the following links:

ResourceLocation
SDK API documentation SDK API docs
GitHub repositoryClient Side SDK for MiniProgram
Sample applicationsDemo code
Published modulenpm

Try Out Demo Code

We provide a runnable demo code for you to understand how FeatureProbe SDK is used.

  1. First, you need to choose which environment FeatureProbe is connected to control your program

  2. Download this repo and run the demo program:

git clone https://github.com/FeatureProbe/client-sdk-miniprogram.git
cd client-sdk-miniprogram
  1. Modify the link information in the example.

    • For online demo environment:

      client_sdk_key snapshot

    • For local docker environment:

      • remoteUrl = "http://YOUR_DOCKER_IP:4009/server"
      • clientSdkKey = "client-25614c7e03e9cb49c0e96357b797b1e47e7f2dff"
  2. Run the program.

Step-by-Step Guide

In this guide we explain how to use feature toggles in a Wechat MiniProgram application using FeatureProbe.

Step 1. Install MiniProgram SDK

Install the FeatureProbe SDK as a dependency in your application.

npm install featureprobe-client-sdk-miniprogram --save

Step 2. Initialize SDK client

Initialize SDK client with required options

import { initialize, FPUser } from 'featureprobe-client-sdk-miniprogram';

const user = new FPUser();
user.with("userId", /* userId */);

const featureProbeClient = initialize({
remoteUrl: "https://featureprobe.io/server",
// remoteUrl: "https://127.0.0.1:4007", // for local docker
clientSdkKey: /* clientSdkKey */
user,
});

featureProbeClient.start();

Step 3. Get varation value

You can get variation value from globalData object, SDK will update globalData.toggles object automatically

const app = getApp();   // getApp is a global function provided Wechat Miniprogram

const value = app.globalData.toggles[/* toggleKey */].value;
const reason = app.globalData.toggles[/* toggleKey */].reason;

You can also use the SDK API to get value or detail.

featureProbeClient.on("ready", function() {
const result = featureProbeClient.boolValue(/* toggleKey */, false);
if (result) {
do_some_thing();
} else {
do_other_thing();
}
const reason = featureProbeClient.boolDetail(/* toggleKey */, false);
console.log(reason);
})

Track events

note

Mini Program SDK supports event tracking from version 2.0.1.

Track custom events

After the SDK is ready, call the track api.

featureProbeClient.on('ready', function() {
// Send a custom event.
// The first parameter is the event name,
// the second parameter is optional, it means a metric value to track
featureProbeClient.track('YOUR_CUSTOM_EVENT_NAME_1');
featureProbeClient.track('YOUR_CUSTOM_EVENT_NAME_2', 5.5);
})

Available options for initialization

This SDK takes the following options:

optionrequireddefaultdescription
remoteUrldependsn/aThe unified URL to get toggles and post events
togglesUrlnon/aThe specific URL to get toggles, once set, remoteUrl will be ignored
eventsUrlnon/aThe specific URL to post events, once set, remoteUrl will be ignored
clientSdkKeyyesn/aThe Client SDK Key is used to authentification
useryesn/aThe User with attributes like name, age is used when toggle evaluation
refreshIntervalno1000The SDK check for updated in millisecond
timeoutIntervalno10000Timeout for SDK initialization, SDK will emit an error event when timeout is reaching